/*
 * Copyright (C), 2002-2017, 苏宁易购电子商务有限公司
 * FileName: HqTask.java
 * Author:   15050536
 * Date:     2017年1月6日 上午9:23:17
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.po.mainpushnew;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * 总部主推任务实体
 *
 * @author 15050536 石键平
 */
@Entity(name = "hq_task")
public class HqTask implements Serializable {

	/**
	 */
	private static final long serialVersionUID = -1517162486033531869L;

	/**
	 * 主键
	 */
	private Long id;

	/**
	 * 月份 格式：201701
	 */
	private String month;

	/**
	 * 类目编码 100001等
	 */
	private String cateCode;

	/**
	 * 类目名称 黑电，冰洗等
	 */
	private String cateName;

	/**
	 * 占比 范围 0.001 到 1.000
	 * 
	 * 0.1% 到 100%
	 */
	private BigDecimal rate;

	/**
	 * 任务主题
	 */
	private String taskTopic;

	/**
	 * 创建时间
	 */
	private Date createTime;

	/**
	 * 任务状态 0：创建中(默认)，1：创建完成
	 */
	private Short taskStatus = 0;

	/**
	 * 0:已结束；1:进行中；2:未开始
	 * 
	 */
	private Integer runStatus;

	/**
	 * 格式化month
	 */
	private String monthFormat;

	public HqTask() {
	}

	/**
	 * @param cateCode
	 * @param cateName
	 * @param rate
	 */
	public HqTask(String month, String cateCode, String cateName, BigDecimal rate) {
		super();
		this.month = month;
		this.cateCode = cateCode;
		this.cateName = cateName;
		this.rate = rate;
		this.taskTopic = getTopic(month, cateName);
	}

	private String getTopic(String month, String cateName) {
		if (month == null || month.length() != 6) {
			return null;
		}
		String tmplete = "%s月%s主推任务";
		return String.format(tmplete, Integer.parseInt(month.substring(4)), cateName);
	}

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "month")
	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
		setRunStatus(month);
	}

	@Column(name = "cate_code")
	public String getCateCode() {
		return cateCode;
	}

	public void setCateCode(String cateCode) {
		this.cateCode = cateCode;
	}

	@Column(name = "cate_name")
	public String getCateName() {
		return cateName;
	}

	public void setCateName(String cateName) {
		this.cateName = cateName;
	}

	@Column(name = "rate")
	public BigDecimal getRate() {
		return rate;
	}

	public void setRate(BigDecimal rate) {
		this.rate = rate;
	}

	@Column(name = "task_topic")
	public String getTaskTopic() {
		return taskTopic;
	}

	public void setTaskTopic(String taskTopic) {
		this.taskTopic = taskTopic;
	}

	@Column(name = "create_time")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "task_status")
	public Short getTaskStatus() {
		return taskStatus;
	}

	public void setTaskStatus(Short taskStatus) {
		this.taskStatus = taskStatus;
	}

	public Integer getRunStatus() {
		return runStatus;
	}

	public void setRunStatus(String month) {
		int m = Integer.parseInt(month);
		Calendar c = Calendar.getInstance();
		int currentYear = c.get(Calendar.YEAR);
		int currentMonth = c.get(Calendar.MONTH) + 1;
		String thisMonth = null;
		if (currentMonth < 10) {
			thisMonth = "0" + currentMonth;
		} else {
			thisMonth = String.valueOf(currentMonth);
		}
		int thisYM = Integer.parseInt(currentYear + thisMonth);
		if (m > thisYM) { // 未开始
			this.runStatus = 2;
		} else if (m == thisYM) { // 进行中
			this.runStatus = 1;
		} else {// 已结束
			this.runStatus = 0;
		}
	}

	public String getMonthFormat() {
		return monthFormat;
	}

	public void setMonthFormat(String monthFormat) {
		this.monthFormat = monthFormat;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
